package indi.h.targetcall.exceptions;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

public class DataConflictException extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Object> lock_context;
	private ReentrantLock rl;

	public DataConflictException() {
		this("");
	}

	public DataConflictException(String msg) {
		super(msg);
		this.rl = new ReentrantLock();
		this.lock_context = new ArrayList<Object>();
	}

	public void lock(Object o) throws DataConflictException {
		final ReentrantLock r = rl;
		try {
			r.lock();
			if (lock_context.contains(o))
				throw this;
			else
				lock_context.add(o);
		} finally {
			r.unlock();
		}
	}

	public void unlock(Object o) {
		final ReentrantLock r = rl;
		try {
			r.lock();
			lock_context.remove(o);
		} finally {
			r.unlock();
		}
	}

}
